package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.rank.EscortWeekUnionValue;
import com.yanqu.road.entity.beautyescort.rank.EscortWeekUserValue;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_GET_UNION_RANK_LIST, desc = "商会榜单")
public class BeautyEscortUnionRankListCmd extends AbstractCommand {

    int type;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortUnionWeekRankListReqMsg reqMsg = BeautyEscortProto.BeautyEscortUnionWeekRankListReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(type != 0 && type != 1){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        int weekId;
        String unionUid = "";
        if(type == 0){
            weekId = BeautyEscortMgr.getPreWeekId();
            EscortWeekUserValue weekUserValue = BeautyEscortMgr.getEscortWeekUserValue(weekId, player.getUserId());
            if(weekUserValue != null){
                unionUid = weekUserValue.getUnionUid();
            }
        }else {
            weekId = BeautyEscortMgr.getThisWeekId();
            unionUid = player.getUserInfo().getUnionUid();
        }
        BeautyEscortProto.CrossBeautyEscortUnionWeekRankListReqMsg.Builder reqMsg = BeautyEscortProto.CrossBeautyEscortUnionWeekRankListReqMsg.newBuilder();
        reqMsg.setUnionUid(unionUid);
        reqMsg.setWeekId(weekId);
        EscortWeekUnionValue unionValue =  BeautyEscortMgr.getEscortWeekUnionValueNullable(weekId, unionUid);
        if(unionValue != null) {
            reqMsg.setValue(String.valueOf(unionValue.getValue()));
        }else {
            reqMsg.setValue("0");
        }
        EscortWeekUserValue userValue = BeautyEscortMgr.getEscortWeekUserValue(weekId, player.getUserId());
        if(userValue != null){
            reqMsg.setGetReward(userValue.isGetReward());
        }else {
            reqMsg.setGetReward(false);
        }
        player.sendPacket(Protocol.C_BEAUTY_ESCORT_GET_UNION_RANK_LIST, reqMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
